import { fileURLToPath, URL } from 'node:url';
import { defineConfig , loadEnv} from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import ViteComponents from "unplugin-vue-components/vite";
import legacy from '@vitejs/plugin-legacy';
import {
  ElementPlusResolver
} from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default ({mode})=>{
  return defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      ViteComponents({
        resolvers:[ElementPlusResolver()]
      }),
      legacy({
        targets: ['defaults', 'not IE 11']
      }),
    ],
    css: {
      preprocessorOptions: {
        sass: {
          additionalData: `@import "./src/assets/scss/main.scss";` // 加载全局样式，使用scss特性
        }
      },
      loaderOptions:{
        scss: {
          sassOtions:{
            outputStyle:"expanded"
          },
          perpendData:`@import "./src/assets/scss/main.scss";`
        }
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      host: "0.0.0.0",
      port: 9090,
      open: true,
      proxy: {

      },
    },
    base: "./",
    build: {
      minify:"terser",
      terserOptions:{
        compress:{
          drop_console: mode !== 'development',
          drop_debugger: mode !== 'development'
        }
      },
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                  .toString()
                  .split("node_modules/")[1]
                  .split("/")[0]
                  .toString();
            }
          },
        },
      },
    }
  })
}
